package com.yml.mall.shipping;

import com.yml.mall.shipping.pojo.InsertAndUpdateDTO;
import com.yml.mall.shipping.pojo.ShippingDTO;
import com.yml.mall.user.pojo.UserDO;
import com.yml.mall.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import java.util.List;
import java.util.Map;

import static com.yml.mall.consts.myConst.CURRENT_USER;

/**
 * @ClassName ShippingController
 * @Author Administrator
 * @Date 2020/4/14   16:09
 */
@RestController
@Validated
public class ShippingController {

    private final ShippingService shippingService;
    @Autowired
    public ShippingController(ShippingServiceImpl shippingService){
        this.shippingService=shippingService;
    }
    @PostMapping("/shippings")
    public ResponseVO<Map<String,Integer>> addShipping(@RequestBody @Valid InsertAndUpdateDTO insertAndUpdateDTO, HttpSession session){
        UserDO userDO =(UserDO)session.getAttribute(CURRENT_USER);
        insertAndUpdateDTO.setUserId(userDO.getId());

        return shippingService.addShipping(insertAndUpdateDTO);
    }
    @DeleteMapping("/shippings/{shippingId}")
    public ResponseVO deleteShipping(@PathVariable @Valid @Min(value = 0,message = "shippingId 不能小于0")  Integer shippingId,HttpSession session){
        UserDO userDO =(UserDO)session.getAttribute(CURRENT_USER);
        return shippingService.deleteShipping(shippingId,userDO.getId());
    }

    @PutMapping("/shippings/{shippingId}")

    public ResponseVO updateShipping(@PathVariable @Valid @Min(value = 0,message = "shippingId 不能小于0") Integer shippingId,@RequestBody InsertAndUpdateDTO insertAndUpdateDTO,HttpSession session){
        UserDO userDO =(UserDO)session.getAttribute(CURRENT_USER);
        return  shippingService.updateShipping(shippingId,insertAndUpdateDTO,userDO.getId());
    }

    @GetMapping("/shippings/{shippingId}")
    public ResponseVO detail(@PathVariable @Valid @Min(value = 0,message = "shippingId 不能小于0") Integer shippingId,HttpSession session){
        UserDO userDO =(UserDO)session.getAttribute(CURRENT_USER);
        return shippingService.detail(shippingId,userDO.getId());
    }

    @GetMapping("/shippings")
    public ResponseVO<List<ShippingDTO>> shippingList(HttpSession session){
        UserDO userDO = (UserDO) session.getAttribute(CURRENT_USER);
        return  shippingService.shippingList(userDO.getId());
    }

}
